//! \file CParameterList.cpp
// -----------------------------------------------------------------------------
//! This program is free software; you can redistribute it and/or
//! modify it under the terms of the GNU General Public License
//! as published by the Free Software Foundation; either version 2
//! of the License, or (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program; if not, write to the Free Software Foundation,
//! Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// -----------------------------------------------------------------------------


#include "CParameterList.h"

namespace code_sheriff
{

// =====[ Public Methods ]======================================================


// =============================================================================
// CParameterList::CParameterList
// -----------------------------------------------------------------------------
CParameterList::CParameterList()
{
  // Initially our parameter list is empty.
  clear();
}


// =============================================================================
// CParameterList::~CParameterList
// -----------------------------------------------------------------------------
CParameterList::~CParameterList()
{
  // Nothing.
}


// =============================================================================
// CParameterList::append
// -----------------------------------------------------------------------------
TERROR CParameterList::append(CParameter Parameter)
{
  TERROR Result; // TODO: document

  // Lock our mutex.
  Result = m_Mutex.lock(CMutex::TIMEOUT_FOREVER);
  if (Result != ERROR_NONE)
    { return (Result); }

  // Push our new value to the back of the vector.
  m_List.push_back(Parameter);

  // Unlock our mutex.
  Result = m_Mutex.unlock();

  // Finally, return the result.
  return (Result);
}


// =============================================================================
// CParameterList::appendValueToLast
// -----------------------------------------------------------------------------
TERROR CParameterList::appendValueToLast(std::string Value)
{
  TERROR Result; // TODO: document

  // Validate our arguments.
  if (Value.length() == 0)
    { return (ERROR_INVALID_ARGUMENT); }

  // Lock our mutex.
  Result = m_Mutex.lock(CMutex::TIMEOUT_FOREVER);
  if (Result != ERROR_NONE)
    { return (Result); }

  // Append the value to the last key-value entry.
  m_List.back().appendValue(Value);

  // Unlock our mutex.
  Result = m_Mutex.unlock();

  // Finally, return the result.
  return (Result);
}


// =============================================================================
// CParameterList::get
// -----------------------------------------------------------------------------
TERROR CParameterList::get(TKEY Key, std::string& Value, int Index)
{
  std::vector<CParameter>::iterator Iterator; // TODO: document
  TERROR Result = ERROR_UNKNOWN;              // TODO: document

  // Lock our mutex.
  Result = m_Mutex.lock(CMutex::TIMEOUT_FOREVER);
  if (Result != ERROR_NONE)
    { return (Result); }

  // Iterate through each key-value and print it to the standard output.
  for (Iterator = m_List.begin(); Iterator != m_List.end(); Iterator++)
  {
    if (Iterator->getKey() == Key)
    {
      Result = Iterator->getValue(Value, Index);
      break; // No more iteration.
    }
  }

  // Unlock our mutex.
  Result = m_Mutex.unlock();

  // Finally, return the result.
  return (Result);
}


// =============================================================================
// CParameterList::clear
// -----------------------------------------------------------------------------
void CParameterList::clear(void)
{
  // Clear the parameter list.
  m_List.clear();
}


// =============================================================================
// CParameterList::printAll
// -----------------------------------------------------------------------------
TERROR CParameterList::printAll(void)
{
  std::vector<CParameter>::iterator Iterator; // TODO: document
  TERROR Result;                              // TODO: document

  // Lock our mutex.
  Result = m_Mutex.lock(CMutex::TIMEOUT_FOREVER);
  if (Result != ERROR_NONE)
    { return (Result); }

  // Iterate through each key-value and print it to the standard output.
  for (Iterator = m_List.begin(); Iterator != m_List.end(); Iterator++)
    { Iterator->print(); }

  // Unlock our mutex.
  Result = m_Mutex.unlock();

  // Finally, return the result.
  return (Result);
}


} // namespace code_sheriff
